<?php
namespace web\common\service;

use addons\member\model\MemberUsdtLog;
use addons\member\model\TradingRecord;
use addons\member\model\Balance;
use addons\member\model\MemberAccountModel;

class TronCoin {

	public $client;
	public $byte;


	public function __construct() {
		$this->client = new NodeClient("https://api.trongrid.io");
        $this->byte=bcpow(10,6);
	}

	public function set_byte($byte){
        $this->byte = bcpow(10,$byte);
    }

	public function getTrc20($address, $only_to="true",$contract_address='TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'){
		$payload = [
				'only_to' => $only_to,
				'contract_address' => $contract_address,
			];
		return $this->client->get("/v1/accounts/{$address}/transactions/trc20",$payload);
	}


	public function amountUpdate($address,$user_id,$coin_type){
		$res = $this->getTrc20($address);
		if($res->success){
			$MemberUsdtLog = new MemberUsdtLog();
	        $recordM = new TradingRecord();
	        $BalanceM = new Balance();
	        $userM = new MemberAccountModel();
			foreach ($res->data as $k => $v) {
	            try{
	                $res = $MemberUsdtLog->getDetailByTxid($v->transaction_id);//订单匹配
	                if($res) continue;
	                $amount = $v->value/$this->byte;
	                $BalanceM->startTrans();
	                $re_balance = $BalanceM->updateBalance($user_id, $coin_type, $amount,1);
	                if(!$re_balance){
	                    $BalanceM->rollback();
	                    continue;
	                }
	                $userRecord = $recordM->addRecord($user_id, $amount, $re_balance['before_amount'], $re_balance['amount'], $coin_type,8, 1, 0, '外网转入');
	                if(!$userRecord){
	                    $BalanceM->rollback();
	                    continue;
	                }
	                // $res = $userM->where('id',$user_id)->setInc('recharge_total',$amount);
	                // if(!$res){
	                //     $BalanceM->rollback();
	                //     continue;
	                // }
	                // $res = $userM->where(['id'=>['in',$pid_path.','.$user_id]])->setInc('team_recharge_total',$amount);
	                // if(!$res){
	                //     $BalanceM->rollback();
	                //     continue;
	                // }
	                $update = [
	                	'user_id'=>$user_id,
	                	'hash'=>$v->transaction_id,
	                	'amount'=>$amount,
	                	'from'=>$v->from,
	                	'to'=>$v->to,
	                	'timeStamp'=>$v->block_timestamp/1000,
	                	'is_platform'=>'trc20',
	                ];
	                $res = $MemberUsdtLog->addLog($update);
	                if(!$res){
	                    $BalanceM->rollback();
	                    continue;
	                }
	                $BalanceM->commit();
	                continue;
	            } catch (\Exception $ex) {
	            	echo $ex->getMessage();
	                continue;
	            }
	        }
	        return true;
		}
	}
}
